package com.aaa.vegetablemarketexpress.service.impl;

import com.aaa.vegetablemarketexpress.dao.OriginDao;
import com.aaa.vegetablemarketexpress.entity.Origin;
import com.aaa.vegetablemarketexpress.entity.User;
import com.aaa.vegetablemarketexpress.service.OriginService;
import com.aaa.vegetablemarketexpress.util.CustomException;
import com.aaa.vegetablemarketexpress.util.ResultStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * @FileName: OriginServiceImpl
 * @Description: 商品产地服务实现类
 * @Author: qxh
 * @CreateTime: 2025/9/8 15:28
 * @Version: 1.0.0
 */
@Service
public class OriginServiceImpl implements OriginService {

    @Autowired
    private HttpSession session;
    @Resource
    private OriginDao originDao;
    /**
     * 检查产地代码是否唯一
     * @param originId
     * @return
     */
    @Override
    public Origin selectOriginById(Long originId) {
        return originDao.selectOriginById(originId);
    }
    /**
     * 查询商品产地列表
     * @param origin
     * @return
     */
    @Override
    public List<Origin> selectOriginList(Origin origin) {
        return originDao.selectOriginList(origin);
    }
    /**
     * 插入商品产地
     * @param origin
     * @return
     */
    @Override
    //@Transactional
    public int insertOrigin(Origin origin) {
        origin.setCreateTime(new Date());
        origin.setDelFlag("0");

        // 检查产地名称是否唯一
        if (!"0".equals(checkOriginNameUnique(origin))) {
            throw new CustomException(ResultStatus.FAILED.getStatusCode(), "产地名称已存在");
        }
        origin.setCreateBy(((User)(session.getAttribute("userInfo"))).getUserName() );
        origin.setCreateTime(new Date());
        return originDao.insertOrigin(origin);
    }
    /**
     * 更新商品产地
     * @param origin
     * @return
     */
    @Override
    //@Transactional
    public int updateOrigin(Origin origin) {
        origin.setUpdateTime(new Date());
        // 检查产地名称是否唯一
        if (!"0".equals(checkOriginNameUnique(origin))) {
            throw new CustomException(ResultStatus.FAILED.getStatusCode(), "产地名称已存在");
        }
        origin.setUpdateBy(((User)(session.getAttribute("userInfo"))).getUserName() );
        origin.setUpdateTime(new Date());
        return originDao.updateOrigin(origin);
    }
    /**
     * 删除商品产地
     * @param originId
     * @return
     */
    @Override
    //@Transactional
    public int deleteOriginById(Long originId) {
        // 检查产地是否被使用
        if (originDao.checkOriginUse(originId) > 0) {
            throw new CustomException(ResultStatus.FAILED.getStatusCode(), "产地已被使用，不能删除");
        }
        return originDao.deleteOriginById(originId);
    }
    /**
     * 批量删除商品产地
     * @param originIds
     * @return
     */
    @Override
    //@Transactional
    public int deleteOriginByIds(Long[] originIds) {
        for (Long originId : originIds) {
            // 检查产地是否被使用
            if (originDao.checkOriginUse(originId) > 0) {
                throw new CustomException(ResultStatus.FAILED.getStatusCode(),
                        "产地ID " + originId + " 已被使用，不能删除");
            }
        }
        return originDao.deleteOriginByIds(originIds);
    }

    /**
     * 检查产地名称是否唯一
     * @param origin
     * @return
     */
    @Override
    public String checkOriginNameUnique(Origin origin) {
        Origin info = originDao.selectOriginByName(origin.getOriginName());
        if (info != null && !info.getOriginId().equals(origin.getOriginId())) {
            return "1"; // 已存在
        }
        return "0"; // 不存在
    }
    /**
     * 更新商品产地状态
     * @param originId
     * @param status
     * @return
     */
    @Override
    @Transactional
    public int updateOriginStatus(Long originId, String status) {
        return originDao.updateOriginStatus(originId, status);
    }
    /**
     * 查询所有正常商品产地
     * @return
     */
    @Override
    public List<Origin> selectAllNormalOrigin() {
        return originDao.selectAllNormalOrigin();
    }
    /**
     * 查询商品产地总数
     * @param origin
     * @return
     */
    @Override
    public int countOrigin(Origin origin) {
        return originDao.countOrigin(origin);
    }
}

